package jsyf.oa.service.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.system.TipDao;
import jsyf.oa.entity.doc.MainEntity;
import jsyf.oa.entity.system.TipEntity;
import jsyf.oa.facade.system.TipFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Maqiuqiu on 2018/4/24.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class TipService implements TipFacade{

    @Autowired
    private TipDao tipDao;

    @Override
    public JSONObject getTipByType(String jsonData){
        List<JSONObject>  records = tipDao.getTipByType(jsonData);
        JSONObject result = new JSONObject();
        result.put("records", records);
        return result;
    }

    @Override
    public JSONObject getTipByPage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String content = jsonParams.getString("content");
        String tipType = jsonParams.getString("tipType");
        String createTime = jsonParams.getString("createTime");
        String createUserId = jsonParams.getString("createUserId");
        String delFlag = jsonParams.getString("delFlag");
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;

        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();

        if (!StringUtils.isBlank(content)) {
            condition.append(" AND content = ?");
            sqlArgs.add(content);
        }
        if (!StringUtils.isBlank(tipType)) {
            condition.append(" AND tipType = ?");
            sqlArgs.add(tipType);
        }
        if (!StringUtils.isBlank(createTime)) {
            condition.append(" AND createTime = ?");
            sqlArgs.add(createTime);
        }
        if (!StringUtils.isBlank(createUserId)) {
            condition.append(" AND createUserId = ?");
            sqlArgs.add(createUserId);
        }
        if (!StringUtils.isBlank(delFlag)) {
            condition.append(" AND delFlag = ? ");
            sqlArgs.add(delFlag);
        }
        condition.append(" AND DEL_FLAG = 0 ");
        //查询list
        List<JSONObject> records = tipDao.getJSONListByPage(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        Integer recordCount = tipDao.getPageCount(condition.toString(), sqlArgs.toArray());
        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    @Override
    public String save(String jsonData) {
        TipEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), TipEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = tipDao.add(entity);
        } else {
            TipEntity tipEntity = (TipEntity) tipDao.getIBaseEntityById(entity.getId());
            tipEntity.setContent(entity.getContent());
            tipEntity.setTipType(entity.getTipType());
            tipEntity.setCreateUserId(entity.getCreateUserId());
            tipEntity.setCreateTime(entity.getCreateTime());
            tipEntity.setDelFlag(entity.getDelFlag());
            rows = tipDao.update(tipEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public Boolean delById(String id) {
        return tipDao.delById(id) > 0;
    }

}
